package com.tansun.easycare.rule.common.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.tansun.easycare.core.service.impl.CrudServiceImpl;
import com.tansun.easycare.rule.common.dao.RuleBaseDao;
import com.tansun.easycare.rule.common.entity.RuleBaseEntity;
import com.tansun.easycare.rule.common.model.RuleDataCollention;
import com.tansun.easycare.rule.common.model.RuleSetParamInfo;
import com.tansun.easycare.rule.common.service.RuleBaseService;
import com.tansun.easycare.rule.exception.BizException;
import com.tansun.easycare.rule.rulemange.entity.RuleManage;
import com.tansun.easycare.rule.serialno.service.RuleSerialNoService;

public abstract class RuleBaseServiceImpl<D extends RuleBaseDao<T>, T extends RuleBaseEntity<T>> extends CrudServiceImpl<D, T> implements RuleBaseService<T>{
    @Autowired
    private RuleSerialNoService ruleSerialNoService;
    public Map<String,String> funcParamMap = new HashMap<String,String>();
    


    public void insertBatch(List<T> list) throws BizException {
        if (list == null || list.size() == 0)
            return;
 
        for (T bo : list) {
            if (bo.getIsNewRecord()) {
                bo.preInsert();
            } else {
                bo.preUpdate();
            }


        }
        int  i =0;
      
        try {
            RuleDataCollention<T> ruleData = new RuleDataCollention<T>();
            int  len  = list.size() ;
            if (len <50) {
            	 ruleData.setList(list);
                 this.dao.insertBatch(ruleData);
            }else {
            	List<T>  tmpList  = new  ArrayList<>();
                for (T t: list) {
                	i++;
                	tmpList.add(t);
                	if (i ==50) {
                		  ruleData.setList(tmpList);
                          this.dao.insertBatch(ruleData);
                          tmpList.clear();
                          i =0;
                	}
                	
                }
                if (tmpList.size()>0) {
            		  ruleData.setList(tmpList);
                      this.dao.insertBatch(ruleData);
                }	
            }

          
        } catch (Exception e) {
            throw new BizException(e, "");
        }

    }
    
    

    public void insertBatch(List<T> list, boolean isInsert) throws BizException {
        if (list == null || list.size() == 0)
            return;
        if (isInsert) {
            for (T bo : list) {
                if (bo.getIsNewRecord()) {
                    bo.preInsert();
                } else {
                    bo.preUpdate();
                }


            }
        }
        try {
            RuleDataCollention<T> ruleData = new RuleDataCollention<T>();
            ruleData.setList(list);
            this.dao.insertBatch(ruleData);
        } catch (Exception e) {
            throw new BizException(e, "");
        }

    }


    public void insertBatch(List<T> list, String[] propertyNames, Object[] propertyValues) throws BizException {
        if (list == null || list.size() == 0)
            return;
        for (T bo : list) {
            if (bo.getIsNewRecord()) {
                try {
                    for (int i = 0; i < propertyNames.length; i++) {
                        PropertyUtils.setSimpleProperty(bo, propertyNames[i], propertyValues[i]);
                    }
                } catch (Exception e) {
                    throw new BizException(e, "");
                }
                bo.preInsert();
            } else {
                bo.preUpdate();
            }


        }
        RuleDataCollention<T> ruleData = new RuleDataCollention<T>();
        ruleData.setList(list);
        this.dao.insertBatch(ruleData);

    }

    public void insertBatch(List<T> list, String[] propertyNames, Object[] propertyValues, boolean isInsert) throws BizException {
        if (list == null || list.size() == 0)
            return;
        for (T bo : list) {
            if (isInsert) {
                try {
                    for (int i = 0; i < propertyNames.length; i++) {
                        PropertyUtils.setSimpleProperty(bo, propertyNames[i], propertyValues[i]);
                    }
                } catch (Exception e) {
                    throw new BizException(e, "");
                }
                bo.preInsert();
            } else {
                bo.preUpdate();
            }
        }
        RuleDataCollention<T> ruleData = new RuleDataCollention<T>();
        ruleData.setList(list);
        this.dao.insertBatch(ruleData);


    }

    public String generalCodeByType(String type, String projectCode) throws BizException {
        return ruleSerialNoService.generalCodeByType(type, projectCode);
    }


    public void updateVersion(RuleManage ruleManage) throws BizException {

    }


    @Override
    public void copyRule(RuleManage oldRuleManage, RuleManage ruleManage) throws BizException {
        // TODO Auto-generated method stub

    }
    
	public String genernalWhenStr(String className,String nameen, String type) {
		if(funcParamMap.containsKey(className+"."+nameen)) {
			return funcParamMap.get(className+"."+nameen);
		}else {
			if("ruleflow".equalsIgnoreCase(type)) {
				return "ruleHelper.getValue(dataMap,\"" + className + "." + nameen + "\")";
			}else if("08".equalsIgnoreCase(type)) {
					return "ruleHelper.getValue(dataMap,\"" + className + "." + nameen + "\")";
			}else {
				return "ruleHelper.getValue($insertMap,\"" + className + "." + nameen + "\")";
			}
			
		}
	}
	public String genernalJavaStr(String className,String nameen, String type,String dataType) {
		if(funcParamMap.containsKey(className+"."+nameen)) {
			return funcParamMap.get(className+"."+nameen);
		}else {
			if("BigInteger".equalsIgnoreCase(dataType)) {
				dataType = "Long";
			}else if("BigDecimal".equalsIgnoreCase(dataType)) {
				dataType = "Double";
			}
			if("Date".equals(dataType)) {
				dataType = "String";
			}
			if("ruleflow".equalsIgnoreCase(type)) {
				return "("+dataType+")ruleHelper.getValue(dataMap,\"" + className + "." + nameen + "\")";
			}else if("08".equalsIgnoreCase(type)) {
					return "("+dataType+")ruleHelper.getValue(dataMap,\"" + className + "." + nameen + "\")";
			}else {
				return "("+dataType+")ruleHelper.getValue($insertMap,\"" + className + "." + nameen + "\")";
			}		
		}
	}
	
	@Override
	public void clearVersion(RuleManage rm,List<String> verArr) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateSetParam(String manageId, RuleSetParamInfo newSetParamInfo, RuleSetParamInfo oldSetParamInfo, boolean setFlag) throws Exception {
		// TODO Auto-generated method stub
		
	}

	
	


}
